package innovacion.servlet.idea;

import innovacion.excepcion.DAOExcepcion;
import innovacion.modelo.Idea;
import innovacion.negocio.GestionIdea;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class IdeaInsertarServlet
 */
@WebServlet("/IdeaInsertarServlet")
public class IdeaInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdeaInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String idea= request.getParameter("idea_update");
		String titulo = request.getParameter("titulo");
		String descripcion = request.getParameter("descripcion");
		String clave1 = request.getParameter("clave1");
		String clave2 = request.getParameter("clave2");
		String clave3 = request.getParameter("clave3");
		String clave4 = request.getParameter("clave4");
		String filepath = request.getParameter("documento");
		String boton = request.getParameter("procesar");
		//String filepath = "C:/Users/Paul/Desktop/idea.txt";
		//InputStream documento = new FileInputStream(new File(filepath));			
		GestionIdea negocio = new GestionIdea();		
		
		try {
			if(boton.equalsIgnoreCase("registrar")){
					if(titulo.equalsIgnoreCase("")|| descripcion.equalsIgnoreCase("") ||
						clave1.equalsIgnoreCase("") || clave2.equalsIgnoreCase("") ||
						clave3.equalsIgnoreCase("") || clave4.equalsIgnoreCase("")){
						
						request.setAttribute("mensaje", "0");
						
					}else{
						int val=negocio.validar_titulo(titulo);
						if(val==0){
							negocio.insertar(titulo,descripcion,clave1,clave2,clave3,clave4,filepath);
							request.setAttribute("mensaje","1" );
						}else{
							request.setAttribute("mensaje", "2");
						}					
					}
					RequestDispatcher rd = request.getRequestDispatcher("registrar_idea.jsp");
					rd.forward(request, response);	
				}else{
					if(boton.equalsIgnoreCase("mostrar")){
						
									
								Collection<Idea> lista = negocio.listar_creada();
								if(lista.size()>0){
									request.setAttribute("listar_creada", lista);
									
								}else{
									request.setAttribute("mensaje", "3");
								}
										
							RequestDispatcher rd = request.getRequestDispatcher("registrar_idea.jsp");
							rd.forward(request, response);				
					}else{
						if(boton.equalsIgnoreCase("actualizar")){
							if(titulo.equalsIgnoreCase("")|| descripcion.equalsIgnoreCase("") ||
									clave1.equalsIgnoreCase("") || clave2.equalsIgnoreCase("") ||
									clave3.equalsIgnoreCase("") || clave4.equalsIgnoreCase("")){
									
									request.setAttribute("mensaje", "0");
									
								}else{									
										negocio.actualizar_idea(idea,titulo,descripcion,clave1,clave2,clave3,clave4,filepath);
										request.setAttribute("mensaje","4" );									
								}
								RequestDispatcher rd = request.getRequestDispatcher("registrar_idea.jsp");
								rd.forward(request, response);	
						}
					}
				}
			
			
			//response.sendRedirect(request.getContextPath()	+ "/RolBuscarServlet");
			
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		
		
		
	}

}
